package yznu_Servlet_order;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Order_Utils.Order;
import yznu_Bean_handle.Delete;
import yznu_Bean_handle.Query;
import yznu_Bean_handle.mainStoreOrder;
import yznu_Bean_handle.othersStoreOrder;

/**
 * Servlet implementation class orderManagerSer
 */
@WebServlet("/orderManagerSer")
public class orderManagerSer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public orderManagerSer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean f = false;
		response.setContentType("text/html;charset=utf-8");
		String status = request.getParameter("status");
		String Username = new String(request.getParameter("Username").getBytes("iso-8859-1"),"utf-8");
		request.setAttribute("source", "修改");
		
		if("others".equals(request.getParameter("source"))){
			
			if(status.equals("show")){
				ArrayList<Order> orders = new Query().Query_others_order(Username);
				request.setAttribute("orders", orders);
				String total = new Query().Query_others_perTotal(Username);
				request.setAttribute("total", total);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/orderShow.jsp");
				dispatcher.forward(request, response);
			}else if(status.equals("del")){
		
				f = new Delete().Del_others_order(Username);
				
				if(f){
					request.setAttribute("Msg", "删除成功");
					int currentPage=(Integer) request.getSession().getAttribute("currentPage");
					request.getSession().setAttribute("currentPage", currentPage);
					int countPage=new othersStoreOrder().countPage();
					request.getSession().setAttribute("countPage", countPage);
					ArrayList<Order> orders = new othersStoreOrder().selectAll(currentPage);
					request.setAttribute("orders", orders);
					RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/othersStoreOrder.jsp");
					dispatcher.forward(request, response);
				}else{
					request.setAttribute("Msg", "删除失败");
					int currentPage=(Integer) request.getSession().getAttribute("currentPage");
					request.getSession().setAttribute("currentPage", currentPage);
					int countPage=new othersStoreOrder().countPage();
					request.getSession().setAttribute("countPage", countPage);
					ArrayList<Order> orders = new othersStoreOrder().selectAll(currentPage);
					request.setAttribute("orders", orders);
					RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/othersStoreOrder.jsp");
					dispatcher.forward(request, response);
				}
			}
			
		}else{
			
			if(status.equals("show")){
				ArrayList<Order> orders = new Query().Query_order(Username);
				request.setAttribute("orders", orders);
				
				String total = new Query().Query_perTotal(Username);
				request.setAttribute("total", total);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/orderShow.jsp");
				dispatcher.forward(request, response);
			}else if(status.equals("del")){
		
				f = new Delete().Del_order(Username);
				
				if(f){
					request.setAttribute("Msg", "删除成功");
					int currentPage=(Integer) request.getSession().getAttribute("currentPage");
					request.getSession().setAttribute("currentPage", currentPage);
					int countPage=new mainStoreOrder().countPage();
					request.getSession().setAttribute("countPage", countPage);
					ArrayList<Order> orders = new mainStoreOrder().selectAll(currentPage);
					request.setAttribute("orders", orders);
					RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/mainStoreOrder.jsp");
					dispatcher.forward(request, response);
				}else{
					request.setAttribute("Msg", "删除失败");
					int currentPage=(Integer) request.getSession().getAttribute("currentPage");
					request.getSession().setAttribute("currentPage", currentPage);
					int countPage=new mainStoreOrder().countPage();
					request.getSession().setAttribute("countPage", countPage);
					ArrayList<Order> orders = new mainStoreOrder().selectAll(currentPage);
					request.setAttribute("orders", orders);
					RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/mainStoreOrder.jsp");
					dispatcher.forward(request, response);
				}
			}
		}
			
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
